package com.xiaoniu.lib_keleim.utils;

import com.xiaoniu.lib_keleim.pojo.vos.BaseMessage;
import com.xiaoniu.lib_keleim.pojo.vos.WrapperMessage;
import com.xiaoniu.lib_keleim.spi.Interceptor;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Objects;

public class InterceptorUtil {

    public static void callSendInterceptor(HashMap<InterceptType, Interceptor> mInterceptors,InterceptType type, BaseMessage message) {
        WrapperMessage wrapperMessage = new WrapperMessage();
        wrapperMessage.fromLocal = true;
        wrapperMessage.message = message;
        if (mInterceptors.get(type)!=null) {
            Objects.requireNonNull(mInterceptors.get(type)).intercept(wrapperMessage);
        }
    }

    public static void callSerializationInterceptor(HashMap<InterceptType, Interceptor> mInterceptors,InterceptType type, WrapperMessage wrapperMessage) {
        wrapperMessage.fromLocal = true;
        if (mInterceptors.get(type)!=null) {
            Objects.requireNonNull(mInterceptors.get(type)).intercept(wrapperMessage);
        }
    }

    public static void callEncodeInterceptor(HashMap<InterceptType, Interceptor> mInterceptors,InterceptType type, WrapperMessage wrapperMessage) {
        wrapperMessage.fromLocal = true;
        if (mInterceptors.get(type)!=null) {
            Objects.requireNonNull(mInterceptors.get(type)).intercept(wrapperMessage);
        }
    }

    public static void callDecodeInterceptor(HashMap<InterceptType, Interceptor> mInterceptors,InterceptType type, WrapperMessage wrapperMessage) {
        wrapperMessage.fromLocal = false;
        if (mInterceptors.get(type)!=null) {
            Objects.requireNonNull(mInterceptors.get(type)).intercept(wrapperMessage);
        }
    }

    public static void callParseInterceptor(HashMap<InterceptType, Interceptor> mInterceptors,InterceptType type, WrapperMessage wrapperMessage) {
        wrapperMessage.fromLocal = false;
        if (mInterceptors.get(type)!=null) {
            Objects.requireNonNull(mInterceptors.get(type)).intercept(wrapperMessage);
        }
    }
    public static void callReceiveInterceptor(HashMap<InterceptType, Interceptor> mInterceptors,InterceptType type, WrapperMessage wrapperMessage) {
        wrapperMessage.fromLocal = false;
        if (mInterceptors.get(type)!=null) {
            Objects.requireNonNull(mInterceptors.get(type)).intercept(wrapperMessage);
        }
    }
}
